package spring.cloud.study.controller;

import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

import cn.hutool.core.lang.Console;
import spring.cloud.study.entity.base.ResponseModel;
import spring.cloud.study.service.OrderService;
import spring.cloud.study.service.PayFeignClient;

@RestController
public class OrderController{
	@Autowired
	private OrderService goodService;

	@GetMapping("order/buy")
	public ResponseModel buyGood() {
		int amount = new Random().nextInt(100);
		System.out.println("购买数量：" + amount);
		return goodService.buy(amount);
	}

	@GetMapping("order/buy/timeout")
	@HystrixCommand(fallbackMethod = "orderByFallBack", commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")// 实际业务功能等待时间
																										// 小于配置时间 即可正常运行
	})
	public ResponseModel buyGoodTimeOut() {
		int amount = new Random().nextInt(100);
		System.out.println("购买数量：" + amount);
		return goodService.buyTimeOut(amount);
	}

	/**
	 * 
	 * @Title: orderByFallBack   
	 * @Description: buyGoodTimeOut的服务降级调用
	 * @date: 2021年12月13日 上午10:18:39   
	 * @version:
	 * @return          
	 * @throws
	 */
	public ResponseModel orderByFallBack() {
		return ResponseModel.errorMsg("订单服务降级处理：系统忙碌中，请稍后重试！");
	}
	
	@Autowired
	private PayFeignClient payFeignClient;
	
	@GetMapping("order/buy/feign/{goodId}/{amount}")
	public ResponseModel buyFeignClient(@PathVariable("goodId") String goodId,@PathVariable("amount") int amount) {
		return payFeignClient.buy(goodId, amount);
	}
	
	public static void main(String[] args) {
		//Hutool.printAllUtils();
		Console.log("123");
	}
	
	
}
